package com.beagle.njddapp.ui.presenter;

import com.amap.api.services.core.PoiItem;
import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.LoginBean;
import com.beagle.njddapp.bean.MeshFBean;
import com.beagle.njddapp.bean.RateBean;
import com.beagle.njddapp.bean.StringBean;
import com.beagle.njddapp.bean.WxPayBean;
import com.beagle.njddapp.bean.ZkBean;
import com.beagle.njddapp.bean.owner.JIaYouZhanBean;
import com.beagle.njddapp.bean.owner.JiaYouCheBean;
import com.beagle.njddapp.bean.owner.NjAndOilBean;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.bean.oil.OilBean;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.net.BaseHttpResponse;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.BaseStringResponseBean;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.MapBaseView;
import com.beagle.njddapp.utils.PayControler;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Observable;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class MapPresenter extends BasePresenter<MapBaseView> {
    private NjOwnerApiService njOwnerApiService;
    private OilApiService OilapiService;
    private ApiService apiService;
    private PayControler payControler;

    public MapPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        njOwnerApiService = retrofit.create(NjOwnerApiService.class);
        OilapiService = retrofit.create(OilApiService.class);
        apiService = retrofit.create(ApiService.class);
        payControler=new PayControler(ActivityUtils.getTopActivity());
    }


//    public void getDeviceListRun(int size,int page){
//
//        Gson gson=new Gson();
//        HashMap<String,String> paramsMap=new LinkedHashMap<>();
//        paramsMap.put("state","1");
////        paramsMap.put("latitude", LocationManager.getInstance().getLatitude()+"");
////        paramsMap.put("longitude",LocationManager.getInstance().getLongitude()+"");
//        String strEntity = gson.toJson(paramsMap);
//        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
//        paramsMap.clear();
//        njOwnerApiService.getDeviceList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<NjBean>>() {
//
//
//            @Override
//            public void success(BaseOilHttpListResponse<NjBean> baseOilHttpListResponse) {
//                if(baseOilHttpListResponse!=null&&baseOilHttpListResponse.results!=null){
//                    getView().showRunNj(baseOilHttpListResponse.results);
//                }
//
//            }
//
//            @Override
//            public void error(int code, String message) {
//                getView().onError(message);
//
//            }
//        });
//    }
//    public void getDeviceListStop(int size,int page){
//
//        Gson gson=new Gson();
//        HashMap<String,String> paramsMap=new LinkedHashMap<>();
//        paramsMap.put("state","2");
////        paramsMap.put("latitude", LocationManager.getInstance().getLatitude()+"");
////        paramsMap.put("longitude",LocationManager.getInstance().getLongitude()+"");
//        String strEntity = gson.toJson(paramsMap);
//        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
//        paramsMap.clear();
//        njOwnerApiService.getDeviceList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<NjBean>>() {
//
//
//            @Override
//            public void success(BaseOilHttpListResponse<NjBean> baseOilHttpListResponse) {
//                if(baseOilHttpListResponse!=null&&baseOilHttpListResponse.results!=null){
//                    getView().showStopNj(baseOilHttpListResponse.results);
//                }
//
//            }
//
//            @Override
//            public void error(int code, String message) {
//                getView().onError(message);
//            }
//        });
//    }


    @Override
    public void destroy() {
        super.destroy();
        payControler.clear();
    }

    public void getDeviceAll(int size, int page){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        njOwnerApiService.getDeviceList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse>() {


            @Override
            public void success(BaseOilHttpListResponse baseOilHttpListResponse) {
                if(baseOilHttpListResponse!=null&&baseOilHttpListResponse.results!=null){
                    getView().showAll(baseOilHttpListResponse.results);
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);

            }
        });
    }
    public void queryFillingStationList(int size,int page,String id){
        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("stationId",id);
        paramsMap.put("latitude", LocationManager.getInstance().getLatitude()+"");
        paramsMap.put("longitude",LocationManager.getInstance().getLongitude()+"");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        njOwnerApiService.queryFillingStationList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<JIaYouZhanBean>>() {


            @Override
            public void success(BaseOilHttpListResponse<JIaYouZhanBean> jIaYouZhanBeanBaseOilHttpListResponse) {
                if(jIaYouZhanBeanBaseOilHttpListResponse!=null&&jIaYouZhanBeanBaseOilHttpListResponse.results!=null){
                    getView().showJyz(jIaYouZhanBeanBaseOilHttpListResponse.results);
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }
    public void queryFillingCarList(int size,int page,String id){
        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("carId",id);
        paramsMap.put("latitude", LocationManager.getInstance().getLatitude()+"");
        paramsMap.put("longitude",LocationManager.getInstance().getLongitude()+"");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        njOwnerApiService.queryFillingCarList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<JiaYouCheBean>>() {

            @Override
            public void success(BaseOilHttpListResponse<JiaYouCheBean> jiaYouCheBeanBaseOilHttpListResponse) {
                if(jiaYouCheBeanBaseOilHttpListResponse!=null&&jiaYouCheBeanBaseOilHttpListResponse.results!=null){
                    getView().showJyc(jiaYouCheBeanBaseOilHttpListResponse.results);
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }


    public void myCar(){
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),"{}");
        OilapiService.myCard(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse<OilBean>>() {
            @Override
            public void success(BaseOilHttpResponse<OilBean> oilHttpResponse) {
                LogUtils.d("success");
                if (oilHttpResponse!=null){
                    getView().showOild(oilHttpResponse.result);
                }

            }

            @Override
            public void error(int code, String message) {
                if (getView()!=null){
                    getView().onError(message);
                }
            }
        });
    }
    public void pj(long orderId,int pj){
        Gson gson=new Gson();
        RateBean rateBean=new RateBean();
        rateBean.setRate(pj);
        String strEntity = gson.toJson(rateBean);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        njOwnerApiService.voteOrder(orderId+"",body).compose(RxSchedulers.compose()).subscribe(new Observer<BaseOilHttpResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseOilHttpResponse o) {
                getView().pjSuccess();
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");
            }

            @Override
            public void onComplete() {

            }
        });
    }


    public void jiayou(NjBean njBean, String phone, String address, String count, PoiItem poiItem) {

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("type","createFuelOrder");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.requestFormToken(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<BaseStringResponseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseStringResponseBean baseStringResponseBean) {
                if (baseStringResponseBean.getResult()!=null){
                    realjiayou(njBean,phone,address,count,baseStringResponseBean.getResult(),poiItem);
                }
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");

            }

            @Override
            public void onComplete() {

            }

        });



    }
    public void realjiayou(NjBean njBean , String phone, String address, String count,String token,PoiItem poiItem){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("meshId",njBean.getMeshId()+"");
        paramsMap.put("latitude", poiItem==null?LocationManager.getInstance().getLatitude()+"":poiItem.getLatLonPoint().getLatitude()+"");
        paramsMap.put("longitude",poiItem==null?LocationManager.getInstance().getLongitude()+"":poiItem.getLatLonPoint().getLongitude()+"");
        paramsMap.put("address",address);
        paramsMap.put("expectMeter", count+"");
        paramsMap.put("formToken",token);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        njOwnerApiService.createOrder(body).compose(RxSchedulers.compose()).subscribe(new Observer<BaseStringResponseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseStringResponseBean o) {
                getView().jiayouSuccess();
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");
            }

            @Override
            public void onComplete() {

            }
        });

    }


    public void wxPay(OilOrderBean oilOrderBean,String price){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("outTradeNo",oilOrderBean.getOrderId()+"");
        paramsMap.put("totalAmount",price);
        paramsMap.put("payType","wechat");
        paramsMap.put("description", "加油订单");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.wxOrder(body).compose(RxSchedulers.compose()).subscribe(new Observer<WxPayBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(WxPayBean o) {
                if (o!=null&&o.getResult()!=null){
                    payControler.wxPay(o,oilOrderBean.getOrderId()+"");
                }
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");
            }

            @Override
            public void onComplete() {

            }
        });

    }

    public void aliPay(OilOrderBean oilOrderBean,String price){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("outTradeNo",oilOrderBean.getOrderId()+"");
        paramsMap.put("totalAmount", price);
        paramsMap.put("payType","alipay");
        paramsMap.put("body","支付加油订单");
        paramsMap.put("subject", "加油订单");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.aliOrder(body).compose(RxSchedulers.compose()).subscribe(new Observer<StringBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(StringBean o) {
                if (o!=null&&o.getResult()!=null){
                    payControler.aLipay(o.getResult(),oilOrderBean.getOrderId()+"");
                }
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");
            }

            @Override
            public void onComplete() {

            }
        });

    }




    public void getNjOilOrderDetail(NjBean njBean,String fuelOrderId) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");

        njOwnerApiService.oilOrderDeyail(fuelOrderId+"",body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse<OilOrderBean>>() {
            @Override
            public void success(BaseOilHttpResponse<OilOrderBean> oilOrderBeanBaseOilHttpResponse) {
                if (oilOrderBeanBaseOilHttpResponse!=null&&oilOrderBeanBaseOilHttpResponse.result!=null){
                    OilOrderBean oilOrderBean=oilOrderBeanBaseOilHttpResponse.result;
                    if (oilOrderBean.isPayed()){
                        getView().showOrder(oilOrderBean,njBean,null);
                    }else if("finished".equals(oilOrderBean.getOrderState())){
                        discountMgt(oilOrderBeanBaseOilHttpResponse.result,njBean);
                    }else {
                        getView().showOrder(oilOrderBean,njBean,null);
                    }
                }else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }



    public void discountMgt(OilOrderBean oilOrderBean,NjBean njBean){
        apiService.discountMgt(oilOrderBean.getOrderId()+"").compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse<ZkBean>>() {
            @Override
            public void success(BaseOilHttpResponse<ZkBean> oilHttpResponse) {
                LogUtils.d("success");
                if (oilOrderBean!=null){
                    getView().showOrder(oilOrderBean,njBean,oilHttpResponse==null?null:oilHttpResponse.result);
                }
            }

            @Override
            public void error(int code, String message) {
                if (getView()!=null){
                    getView().onError(message);
                }
            }
        });
    }


    public void cancelOrder(String fuelOrderId) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");

        njOwnerApiService.cancelOrder(fuelOrderId+"",body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse<OilOrderBean>>() {
            @Override
            public void success(BaseOilHttpResponse<OilOrderBean> oilOrderBeanBaseOilHttpResponse) {
                if (oilOrderBeanBaseOilHttpResponse!=null){
                    getView().cancleSuccess();
                }else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }


    public void pay(long orderId){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("type","fuelStartOrderPayment");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.requestFormToken(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<BaseStringResponseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseStringResponseBean baseStringResponseBean) {
                if (baseStringResponseBean.getResult()!=null){

                    Gson gson=new Gson();
                    HashMap<String,String> paramsMap=new LinkedHashMap<>();
                    paramsMap.put("paymentType","5");
                    paramsMap.put("amount", "");
                    paramsMap.put("lmeter","");
                    paramsMap.put("formToken",baseStringResponseBean.getResult());
                    String strEntity = gson.toJson(paramsMap);
                    RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
                    paramsMap.clear();
                    njOwnerApiService.startOrderPayment(orderId+"",body).compose(RxSchedulers.compose()).subscribe(new Observer<BaseOilHttpResponse>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(BaseOilHttpResponse o) {
                            getView().paySuccess();
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().onError("");
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
                }
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");

            }

            @Override
            public void onComplete() {

            }
        });
    }



    public void getPjList(NjBean njBean){
        Gson gson=new Gson();
        MeshFBean meshFBean=new MeshFBean();
        meshFBean.setMeshId(njBean.getMeshId());
        meshFBean.setVoted(false);
        meshFBean.setOrderState("finished");
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),gson.toJson(meshFBean));
        njOwnerApiService.getPjList(10+"",1+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<NjAndOilBean>>() {

            @Override
            public void success(BaseOilHttpListResponse<NjAndOilBean> baseOilHttpListResponse) {
                if (baseOilHttpListResponse==null||baseOilHttpListResponse.results==null||baseOilHttpListResponse.results.size()<=0){
                    getView().showJyDialog(njBean);
                }else {
                    getView().showPjDialog(baseOilHttpListResponse.results.get(0));
                }

            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);

            }
        });
    }
}

